package com.zheng.upms.server.controller.manage;

import com.zheng.common.base.BaseController;
import com.zheng.upms.common.constant.UpmsResult;
import com.zheng.upms.common.constant.UpmsResultConstant;
import com.zheng.upms.dao.model.UpmsScheduleJobLog;
import com.zheng.upms.dao.model.UpmsScheduleJobLogExample;
import com.zheng.upms.rpc.api.UpmsScheduleJobLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lvg EMIAL：<a href="mailto:weiguo.liang@gh-ca.com">weiguo.liang@gh-ca.com</a> <br>
 * @version 1.0 <br>
 * @Company: GH-CA<br>
 * @ClassName: UpmsJobLogController <br>
 * @Description: TODO <br>
 * @date 2019/7/14 18:09<br>
 **/
@Controller
@RequestMapping("/manage/joblog")
@Api(value = "任务调度日志控制器", description = "任务调度日志控制器")
public class UpmsJobLogController extends BaseController {

    @Autowired
    private UpmsScheduleJobLogService upmsScheduleJobLogService;

    @ApiOperation(value = "任务调度Log首页")
    @RequiresPermissions("upms:joblog:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/joblog/index.jsp";
    }

    @ApiOperation(value = "任务调度Log列表")
    @RequiresPermissions("upms:joblog:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order,
            @RequestParam(required = false, value = "jobName") String jobName,
            @RequestParam(required = false, value = "jobGroup") String jobGroup
            ) {
        UpmsScheduleJobLogExample example = new UpmsScheduleJobLogExample();
        UpmsScheduleJobLogExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            example.setOrderByClause(sort + " " + order);
        }else{
            example.setOrderByClause(" create_time desc");
        }
        if(StringUtils.isNotBlank(jobName)){
            criteria.andJobNameLike("%"+jobName+"%");
        }
        if(StringUtils.isNotBlank(jobGroup)){
            criteria.andJobGroupLike("%"+jobGroup+"%");
        }

        List<UpmsScheduleJobLog> rows = upmsScheduleJobLogService.selectByExampleForOffsetPage(example, offset, limit);
        long total = upmsScheduleJobLogService.countByExample(example);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "删除任务调度")
    @RequiresPermissions("upms:job:delete")
    @RequestMapping(value = "/delete/{ids}",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = 0;
        try {
            count = upmsScheduleJobLogService.deleteByPrimaryKeys(ids);
        } catch (Exception e) {
            return new UpmsResult(UpmsResultConstant.FAILED, count);
        }
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }
}
